package com.mzxy.boxuegu.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.LoginActivity;
import com.mzxy.boxuegu.R;
import com.mzxy.boxuegu.utils.AnalysisUtils;

public class MyInfoView {
    public ImageView iv_head_icon;
    private LinearLayout ll_head;
    private RelativeLayout rl_course_history,rl_setting;
    private TextView tv_user_name;
    private Activity mContext;
    private LayoutInflater mInflater;
    private View mCurrntView;
    public MyInfoView(Activity context){
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }
    private void createView(){
        initView();
    }

    private void initView() {
        mCurrntView = mInflater.inflate(R.layout.main_view_myinfo,null);
        ll_head= (LinearLayout) mCurrntView.findViewById(R.id.ll_head);
        iv_head_icon = (ImageView) mCurrntView.findViewById(R.id.iv_head_icon);
        rl_course_history = (RelativeLayout) mCurrntView.findViewById(R.id.rl_course_history);
        rl_setting = (RelativeLayout) mCurrntView.findViewById(R.id.rl_setting);
        tv_user_name = (TextView) mCurrntView.findViewById(R.id.tv_user_name);
        mCurrntView.setVisibility(View.VISIBLE);
        setLoginParams(readLoginStatus());
        ll_head.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (readLoginStatus()){

                }else {
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    mContext.startActivityForResult(intent,1);
                }
            }
        });
        rl_course_history.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                if (readLoginStatus()){

                }else {
                    Toast.makeText(mContext,"您还未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });
        rl_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (readLoginStatus()){

                }else {
                    Toast.makeText(mContext,"您还未登录，请先登录",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setLoginParams(boolean islogin) {
        if (islogin){
            tv_user_name.setText(AnalysisUtils.readLoginUserName(mContext));
        }else {
            tv_user_name.setText("登录");
        }
    }

    public View getView() {
        if (mCurrntView== null){
            createView();
        }
        return mCurrntView;
    }


    public void showView(){
        if (mCurrntView == null){
            createView();
        }
        mCurrntView.setVisibility(View.VISIBLE);
    }

    private boolean readLoginStatus() {
        SharedPreferences sp = mContext.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin =sp.getBoolean("isLogin",false);
        return  isLogin;
    }

}
